package com.gaussian.property.app.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaussian.property.app.vo.Reply;
import com.gaussian.property.app.vo.suggest.HistorySuggest;
import com.gaussian.property.app.vo.suggest.SuggestForm;
import com.gaussian.property.module.biz.entity.*;
import com.gaussian.property.module.biz.service.*;
import com.songqi.common.core.exception.CustomException;
import com.songqi.common.core.utils.BeanCopyUtil;
import com.songqi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2024/4/18 20:26
 */
@Service
public class SuggestService {

    @Autowired
    private IProjectSuggestionService iProjectSuggestionService;
    @Autowired
    private IProjectSuggestionTagService iProjectSuggestionTagService;
    @Autowired
    private IProjectSuggestionReplyService iProjectSuggestionReplyService;
    @Autowired
    private IUserProjectService iUserProjectService;


    /**
     * 提交建议
     * @param suggestForm
     */
    @Transactional(rollbackFor = Exception.class)
    public void submitSuggest(SuggestForm suggestForm) {
        AppUser appUser = (AppUser) SecurityUtils.getTokenUser();
        LambdaQueryWrapper<UserProject> queryWrapper = new LambdaQueryWrapper<UserProject>()
                .eq(UserProject::getProjectId, suggestForm.getProjectId())
                .eq(UserProject::getAppUserId, appUser.getAppUserId());
        if(iUserProjectService.count(queryWrapper) == 0){
            throw new CustomException("【参数错误】当前用户没有绑定此项目id");
        }

        ProjectSuggestion projectSuggestion = new ProjectSuggestion();
        projectSuggestion.setProjectId(suggestForm.getProjectId());
        projectSuggestion.setContent(suggestForm.getSuggestContent());
        projectSuggestion.setAppUserId(appUser.getAppUserId());
        iProjectSuggestionService.save(projectSuggestion);

        if(CollectionUtil.isNotEmpty(suggestForm.getTagIdList())){
            List<ProjectSuggestionTag> projectSuggestionTagList = new ArrayList<>();
            for (String tagId : suggestForm.getTagIdList()) {
                ProjectSuggestionTag projectSuggestionTag = new ProjectSuggestionTag();
                projectSuggestionTag.setProjectSuggestionId(projectSuggestion.getProjectSuggestionId());
                projectSuggestionTag.setTagLibId(tagId);
                projectSuggestionTagList.add(projectSuggestionTag);
            }
            iProjectSuggestionTagService.saveBatch(projectSuggestionTagList);
        }
    }


    /**
     * 查看我的历史建议
     * @param projectId
     * @return
     */
    public List<HistorySuggest> listHistorySuggest(String projectId) {
        List<HistorySuggest> historySuggestList = new ArrayList<>();
        AppUser appUser = (AppUser) SecurityUtils.getTokenUser();
        LambdaQueryWrapper<ProjectSuggestion> queryWrapper= new LambdaQueryWrapper<ProjectSuggestion>()
                .eq(ProjectSuggestion::getAppUserId, appUser.getAppUserId())
                .eq(ProjectSuggestion::getProjectId, projectId)
                .orderByDesc(ProjectSuggestion::getCreateTime);
        List<ProjectSuggestion> list = iProjectSuggestionService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            for (ProjectSuggestion projectSuggestion : list) {
                HistorySuggest historySuggest = new HistorySuggest();
                historySuggest.setContent(projectSuggestion.getContent());
                historySuggest.setCreateTime(projectSuggestion.getCreateTime());
                List<ProjectSuggestionReply> replyList = queryProjectSuggestReply(projectSuggestion.getProjectSuggestionId());
                if(CollectionUtil.isNotEmpty(replyList)){
                    historySuggest.setReplyList(BeanCopyUtil.copyList(replyList, Reply.class));
                }
                historySuggestList.add(historySuggest);
            }
        }
        return historySuggestList;
    }

    private List<ProjectSuggestionReply> queryProjectSuggestReply(String projectSuggestionId) {
        LambdaQueryWrapper<ProjectSuggestionReply> queryWrapper = new LambdaQueryWrapper<ProjectSuggestionReply>()
                .eq(ProjectSuggestionReply::getProjectSuggestionId, projectSuggestionId)
                .orderByDesc(ProjectSuggestionReply::getCreateTime);
        return iProjectSuggestionReplyService.list(queryWrapper);
    }
}
